<%@ page import="service.AccountService" %>
<%@ page import="model.Store" %>
<%@ page import="java.util.List" %>
<%@ page import="top.ConfigCenter" %>
<%@ page import="model.PicturesInfo" %><%--
Created by IntelliJ IDEA.
User: Zafkiel
Date: 2021/8/14
Time: 18:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %>
<!DOCTYPE html>
<html lang="en">
	<head>
		<link rel="stylesheet" type="text/css" href="css/mystore.css">
		<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
        <script type="text/javascript" src="js/account.js"></script>
		<title></title>
	</head>
	<body>
    <%  session.setAttribute("OESAt", "myStorePage");
        AccountService.LoginInfo logInfo = (AccountService.LoginInfo)session.getAttribute("OESloginInfo");
//System.out.println("mystore.jsp: " + (logInfo == null? "null": logInfo.getLoginState()));
//System.out.println(request.getServletContext().getRealPath("/"));
        if(logInfo == null  ||  logInfo.getLoginState() != AccountService.LoginState.SUCCEED  ||  logInfo.getUserInfo() == null) {
            //System.out.println("userInfo is null");
            session.setAttribute("OESErrorCode", "NOTLOGIN");
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        }
    %>
        <div id="contentContainer">

        </div>
        <form id="newStoreData" action="register.store" method="post" enctype="multipart/form-data">
            <input type="hidden" id="userid" name="userid" value="<%=logInfo.getUserInfo().getU_id().toString()%>"><input type="hidden" id="password" name="password" value="<%=logInfo.getUserInfo().getU_passwd()%>">
            <div id="newDialog">
                <div class="newDialogRow"><lable id="newStoreTitle">店铺注册</lable></div>
                <div class="newDialogRow"><input type="text" id="storename" name="storename" placeholder="店铺名称" required></div>
                <div class="newDialogRow"><input type="file" id="storepic" name="storepic" accept="image/png, image/jpeg"><input type="button" id="btnstorepic" value="选择封面" onclick="pictureInput()"></div>
                <div class="newDialogRow">
                    <input type="button" id="ok" value="开业!">
                    <input type="reset" id="cancel" value="算了..">
                </div>
            </div>
        </form>
	</body>
	<script type="text/javascript">

        var container;

        $(function(){
            container = document.getElementById("contentContainer");
            jsInit();
        })

        function pictureInput() {
	        document.getElementById("storepic").click();
	    }

        //新增一个可分行整体
        function appendContentLine(sizeX, n, aDiv) {
            return contentRandomBinaryDividingAppend(sizeX, parseInt(0.8 * sizeX), n, aDiv);
        }

        function contentRandomBinaryDividingAppend(x, y, count, aDiv) {
            if(count > 1) {
                if((parseInt(Math.random() * 10) & 1) == 1) {
                    if(y * 2 / x > 3) y /= 2; else x /= 2;
                }
                else {
                    if(x * 2 / y > 3) x /= 2; else y /= 2;
                }
            }

            if(!appendAContent(x, y, true, aDiv)) return false;

            if(count > 1) return contentRandomBinaryDividingAppend(x, y, --count);
            return true;
        }

        function appendAContent(sizeX, sizeY, left, aDiv) {
            var newDiv;

            if(aDiv != null) {
                newDiv = aDiv;
                newDiv.css("float", (left? "left": "right"));
                newDiv.css("height", (sizeY-12) + "px");
                newDiv.css("width", (sizeX-12) + "px");
                newDiv.css("width", (sizeX-12) + "px");
                newDiv.addClass("content");
            } else newDiv = $("<div class=\"content\" style=\"float:" + (left? "left": "right") + "; height:" + (sizeY-12) + "px; width: " + (sizeX-12) + "px;\"></div>");


            $("#contentContainer").append(newDiv);
            return true;
        }

        function addPlusButton(areaWidth) {
            var divPlus = $("<div id=\"divPlus\" class=\"plus\"></div>");

            divPlus.click(function(){
                $("#newDialog").css("display", "flex");
            });

            appendContentLine(parseInt(areaWidth * 0.31), 1, divPlus);
        }

        function divStoreClick(which) {
            alert(which);

            /*用location.href发get请求到店铺详情页面，在详情页面中检查session.getAttribute("OESloginInfo")*/
        }

        function addStore(storeID, storeName, storeIndexPic) {
            var divStore = $("<div class=\"store\" onclick=\"divStoreClick('" + storeID + "')\"><label class=\"storeNameShow\">" + storeName + "</label></div>");

            if(storeIndexPic != null) divStore.css("background-image", "url(\"" + storeIndexPic + "\")");
            appendContentLine(parseInt(container.clientWidth * 0.31), 1, divStore);
        }

        function jsInit() {
            /*alert(JSON.parse("{\"a\":\"1\",\"b\":\"2\"}")["a"]);*/

            //for(var i = 0; i < 3; ++i) appendContentLine(parseInt(container.clientWidth * 0.31), 1, null);

            <%
            List<Store> myStores = ConfigCenter.getStoreService().getStories(logInfo.getUserInfo().getU_id(), logInfo.getUserInfo().getU_passwd());

            String thisPic = null;
            for(Store each : myStores) {

                //获取封面图片名称
                PicturesInfo thisStorePictureInfo = new PicturesInfo(each.getSt_pic_address());
                thisPic = thisStorePictureInfo.getIndex();

                //拼接封面图地址http://localhost:8080/OurEStore_war/data/store_pictures/%E6%B5%8B%E8%AF%95/82347323_p0.png
                if(thisPic != null) thisPic = request.getContextPath() + "/data/store_pictures/" + each.getSt_name() + '/' + thisPic;

                %> addStore("<%=each.getSt_id()%>", "<%=each.getSt_name()%>", <%if(thisPic == null) {%>null<%} else {%>"<%=thisPic%>"<%}%>);<%
            }
            %>

            addPlusButton(container.clientWidth);

            $("#cancel").click(function() {
                $("#newDialog").css("display", "none");
                $("#newDialog").css("background-image", "url(\"img/blank.png\")");
                //.getElementById("storepic").value = "";
            });

            $("#storepic").change(function(){
                var files = this.files;
                if(files.length == 1) {
                    $("#newDialog").css("background-image", "url(\"" + URL.createObjectURL(files[0]) + "\")");
                } else {
                    $("#newDialog").css("background-image", "url(\"img/blank.png\")");
                }
            });

            $("#ok").click(function () {
                document.getElementById("newStoreData").submit();
            });
        }
	</script>
</html>